1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file tr1/exp_integral.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{tr1/cmath}
32 // ISO C++ 14882 TR1: 5.2 Special functions
35 // Written by Edward Smith-Rowland based on:
37 // (1) Handbook of Mathematical Functions,
38 // Ed. by Milton Abramowitz and Irene A. Stegun,
39 // Dover Publications, New-York, Section 5, pp. 228-251.
40 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
41 // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
42 // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
43 // 2nd ed, pp. 222-225.
46 #ifndef _GLIBCXX_TR1_EXP_INTEGRAL_TCC
47 #define _GLIBCXX_TR1_EXP_INTEGRAL_TCC 1
49 #include "special_function_util.h"
51 namespace std _GLIBCXX_VISIBILITY(default)
55 // [5.2] Special functions
57 // Implementation-space details.
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 * @brief Return the exponential integral @f$ E_1(x) @f$
64 * by series summation. This should be good
67 * The exponential integral is given by
69 * E_1(x) = \int_{1}^{\infty} \frac{e^{-xt}}{t} dt
72 * @param __x The argument of the exponential integral function.
73 * @return The exponential integral.
75 template<typename _Tp>
77 __expint_E1_series(const _Tp __x)
79 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
83 const unsigned int __max_iter = 100;
84 for (unsigned int __i = 1; __i < __max_iter; ++__i)
86 __term *= - __x / __i;
87 if (std::abs(__term) < __eps)
90 __esum += __term / __i;
92 __osum += __term / __i;
95 return - __esum - __osum
96 - __numeric_constants<_Tp>::__gamma_e() - std::log(__x);
101 * @brief Return the exponential integral @f$ E_1(x) @f$
102 * by asymptotic expansion.
104 * The exponential integral is given by
106 * E_1(x) = \int_{1}^\infty \frac{e^{-xt}}{t} dt
109 * @param __x The argument of the exponential integral function.
110 * @return The exponential integral.
112 template<typename _Tp>
114 __expint_E1_asymp(const _Tp __x)
119 const unsigned int __max_iter = 1000;
120 for (unsigned int __i = 1; __i < __max_iter; ++__i)
123 __term *= - __i / __x;
124 if (std::abs(__term) > std::abs(__prev))
126 if (__term >= _Tp(0))
132 return std::exp(- __x) * (__esum + __osum) / __x;
137 * @brief Return the exponential integral @f$ E_n(x) @f$
138 * by series summation.
140 * The exponential integral is given by
142 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
145 * @param __n The order of the exponential integral function.
146 * @param __x The argument of the exponential integral function.
147 * @return The exponential integral.
149 template<typename _Tp>
151 __expint_En_series(const unsigned int __n, const _Tp __x)
153 const unsigned int __max_iter = 100;
154 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
155 const int __nm1 = __n - 1;
156 _Tp __ans = (__nm1 != 0
157 ? _Tp(1) / __nm1 : -std::log(__x)
158 - __numeric_constants<_Tp>::__gamma_e());
160 for (int __i = 1; __i <= __max_iter; ++__i)
162 __fact *= -__x / _Tp(__i);
165 __del = -__fact / _Tp(__i - __nm1);
168 _Tp __psi = -__numeric_constants<_Tp>::gamma_e();
169 for (int __ii = 1; __ii <= __nm1; ++__ii)
170 __psi += _Tp(1) / _Tp(__ii);
171 __del = __fact * (__psi - std::log(__x));
174 if (std::abs(__del) < __eps * std::abs(__ans))
177 std::__throw_runtime_error(__N("Series summation failed "
178 "in __expint_En_series."));
183 * @brief Return the exponential integral @f$ E_n(x) @f$
184 * by continued fractions.
186 * The exponential integral is given by
188 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
191 * @param __n The order of the exponential integral function.
192 * @param __x The argument of the exponential integral function.
193 * @return The exponential integral.
195 template<typename _Tp>
197 __expint_En_cont_frac(const unsigned int __n, const _Tp __x)
199 const unsigned int __max_iter = 100;
200 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
201 const _Tp __fp_min = std::numeric_limits<_Tp>::min();
202 const int __nm1 = __n - 1;
203 _Tp __b = __x + _Tp(__n);
204 _Tp __c = _Tp(1) / __fp_min;
205 _Tp __d = _Tp(1) / __b;
207 for ( unsigned int __i = 1; __i <= __max_iter; ++__i )
209 _Tp __a = -_Tp(__i * (__nm1 + __i));
211 __d = _Tp(1) / (__a * __d + __b);
212 __c = __b + __a / __c;
213 const _Tp __del = __c * __d;
215 if (std::abs(__del - _Tp(1)) < __eps)
217 const _Tp __ans = __h * std::exp(-__x);
221 std::__throw_runtime_error(__N("Continued fraction failed "
222 "in __expint_En_cont_frac."));
227 * @brief Return the exponential integral @f$ E_n(x) @f$
228 * by recursion. Use upward recursion for @f$ x < n @f$
229 * and downward recursion (Miller's algorithm) otherwise.
231 * The exponential integral is given by
233 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
236 * @param __n The order of the exponential integral function.
237 * @param __x The argument of the exponential integral function.
238 * @return The exponential integral.
240 template<typename _Tp>
242 __expint_En_recursion(const unsigned int __n, const _Tp __x)
245 _Tp __E1 = __expint_E1(__x);
248 // Forward recursion is stable only for n < x.
250 for (unsigned int __j = 2; __j < __n; ++__j)
251 __En = (std::exp(-__x) - __x * __En) / _Tp(__j - 1);
255 // Backward recursion is stable only for n >= x.
257 const int __N = __n + 20; // TODO: Check this starting number.
259 for (int __j = __N; __j > 0; --__j)
261 __En = (std::exp(-__x) - __j * __En) / __x;
265 _Tp __norm = __En / __E1;
273 * @brief Return the exponential integral @f$ Ei(x) @f$
274 * by series summation.
276 * The exponential integral is given by
278 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
281 * @param __x The argument of the exponential integral function.
282 * @return The exponential integral.
284 template<typename _Tp>
286 __expint_Ei_series(const _Tp __x)
290 const unsigned int __max_iter = 1000;
291 for (unsigned int __i = 1; __i < __max_iter; ++__i)
294 __sum += __term / __i;
295 if (__term < std::numeric_limits<_Tp>::epsilon() * __sum)
299 return __numeric_constants<_Tp>::__gamma_e() + __sum + std::log(__x);
304 * @brief Return the exponential integral @f$ Ei(x) @f$
305 * by asymptotic expansion.
307 * The exponential integral is given by
309 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
312 * @param __x The argument of the exponential integral function.
313 * @return The exponential integral.
315 template<typename _Tp>
317 __expint_Ei_asymp(const _Tp __x)
321 const unsigned int __max_iter = 1000;
322 for (unsigned int __i = 1; __i < __max_iter; ++__i)
326 if (__term < std::numeric_limits<_Tp>::epsilon())
328 if (__term >= __prev)
333 return std::exp(__x) * __sum / __x;
338 * @brief Return the exponential integral @f$ Ei(x) @f$.
340 * The exponential integral is given by
342 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
345 * @param __x The argument of the exponential integral function.
346 * @return The exponential integral.
348 template<typename _Tp>
350 __expint_Ei(const _Tp __x)
353 return -__expint_E1(-__x);
354 else if (__x < -std::log(std::numeric_limits<_Tp>::epsilon()))
355 return __expint_Ei_series(__x);
357 return __expint_Ei_asymp(__x);
362 * @brief Return the exponential integral @f$ E_1(x) @f$.
364 * The exponential integral is given by
366 * E_1(x) = \int_{1}^\infty \frac{e^{-xt}}{t} dt
369 * @param __x The argument of the exponential integral function.
370 * @return The exponential integral.
372 template<typename _Tp>
374 __expint_E1(const _Tp __x)
377 return -__expint_Ei(-__x);
378 else if (__x < _Tp(1))
379 return __expint_E1_series(__x);
380 else if (__x < _Tp(100)) // TODO: Find a good asymptotic switch point.
381 return __expint_En_cont_frac(1, __x);
383 return __expint_E1_asymp(__x);
388 * @brief Return the exponential integral @f$ E_n(x) @f$
389 * for large argument.
391 * The exponential integral is given by
393 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
396 * This is something of an extension.
398 * @param __n The order of the exponential integral function.
399 * @param __x The argument of the exponential integral function.
400 * @return The exponential integral.
402 template<typename _Tp>
404 __expint_asymp(const unsigned int __n, const _Tp __x)
408 for (unsigned int __i = 1; __i <= __n; ++__i)
411 __term *= -(__n - __i + 1) / __x;
412 if (std::abs(__term) > std::abs(__prev))
417 return std::exp(-__x) * __sum / __x;
422 * @brief Return the exponential integral @f$ E_n(x) @f$
425 * The exponential integral is given by
427 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
430 * This is something of an extension.
432 * @param __n The order of the exponential integral function.
433 * @param __x The argument of the exponential integral function.
434 * @return The exponential integral.
436 template<typename _Tp>
438 __expint_large_n(const unsigned int __n, const _Tp __x)
440 const _Tp __xpn = __x + __n;
441 const _Tp __xpn2 = __xpn * __xpn;
444 for (unsigned int __i = 1; __i <= __n; ++__i)
447 __term *= (__n - 2 * (__i - 1) * __x) / __xpn2;
448 if (std::abs(__term) < std::numeric_limits<_Tp>::epsilon())
453 return std::exp(-__x) * __sum / __xpn;
458 * @brief Return the exponential integral @f$ E_n(x) @f$.
460 * The exponential integral is given by
462 * E_n(x) = \int_{1}^\infty \frac{e^{-xt}}{t^n} dt
464 * This is something of an extension.
466 * @param __n The order of the exponential integral function.
467 * @param __x The argument of the exponential integral function.
468 * @return The exponential integral.
470 template<typename _Tp>
472 __expint(const unsigned int __n, const _Tp __x)
474 // Return NaN on NaN input.
476 return std::numeric_limits<_Tp>::quiet_NaN();
477 else if (__n <= 1 && __x == _Tp(0))
478 return std::numeric_limits<_Tp>::infinity();
481 _Tp __E0 = std::exp(__x) / __x;
485 _Tp __E1 = __expint_E1(__x);
490 return _Tp(1) / static_cast<_Tp>(__n - 1);
492 _Tp __En = __expint_En_recursion(__n, __x);
500 * @brief Return the exponential integral @f$ Ei(x) @f$.
502 * The exponential integral is given by
504 * Ei(x) = -\int_{-x}^\infty \frac{e^t}{t} dt
507 * @param __x The argument of the exponential integral function.
508 * @return The exponential integral.
510 template<typename _Tp>
512 __expint(const _Tp __x)
515 return std::numeric_limits<_Tp>::quiet_NaN();
517 return __expint_Ei(__x);
520 _GLIBCXX_END_NAMESPACE_VERSION
521 } // namespace std::tr1::__detail
525 #endif // _GLIBCXX_TR1_EXP_INTEGRAL_TCC